//
//  MCChatGPTIMVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/19.
//

import UIKit
import SnapKit

class MCChatGPTIMVc: MCBaseVc {
     
    let imTableViewIdentifier = "imTableViewIdentifier"
    
    
    var imTableView:UITableView!
    
    var imChatInputView:QMUITextView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
      
        self.navigationItem.title = "AI运动助手"
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        imTableView = UITableView.init(frame: CGRect.zero, style: .plain)
        imTableView.dataSource = self
        imTableView.delegate = self
        imTableView.separatorStyle = .none
        imTableView.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        imTableView.keyboardDismissMode = .onDrag
        self.view.addSubview(imTableView)
        imTableView.snp.makeConstraints { make in
            make.left.equalTo(view.snp.left).offset(0)
            make.right.equalTo(view.snp.right).offset(0)
            make.top.equalTo(view.snp.top).offset(0)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-60)
        }
        
        imTableView.register(UINib(nibName: "MCChatGPTIMCell", bundle: Bundle.main), forCellReuseIdentifier: imTableViewIdentifier)
        
        imChatInputView = QMUITextView.init()
        
        imChatInputView.delegate = self
        imChatInputView.contentInsetAdjustmentBehavior = .never
        imChatInputView.placeholder = "请输入您的问题"
        imChatInputView.textContainerInset = UIEdgeInsets(top: 10, left: 7, bottom: 10, right: 7)
        imChatInputView.returnKeyType = .send
        imChatInputView.enablesReturnKeyAutomatically = true
       // imChatInputView.typingAttributes = {.font:UIFont.systemFont(ofSize: 15),}
        imChatInputView.backgroundColor = UIColor.qmui_color(withHexString: "#666666")
        imChatInputView.maximumHeight = 150
        imChatInputView.font = UIFont.systemFont(ofSize: 14)
        imChatInputView.maximumTextLength = 100
        self.view.addSubview(imChatInputView)
        
        
        
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        let padding = UIEdgeInsets(top: imTableView.qmui_bottom, left: 16 + self.view.safeAreaInsets.left, bottom: 16 + self.view.safeAreaInsets.bottom, right: 16 + self.view.safeAreaInsets.right)
        let contentWidth = CGRectGetWidth(self.view.bounds) - UIEdgeInsetsGetHorizontalValue(padding);
        let textViewSize = self.imChatInputView.sizeThatFits(CGSize(width: contentWidth, height:Double.infinity))
        self.imChatInputView.frame = CGRectMake(padding.left, padding.top, CGRectGetWidth(self.view.bounds) - UIEdgeInsetsGetHorizontalValue(padding), fmax(textViewSize.height, 50));
    }
    
}

extension MCChatGPTIMVc:UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 130
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:MCChatGPTIMCell = tableView.dequeueReusableCell(withIdentifier: imTableViewIdentifier, for: indexPath) as! MCChatGPTIMCell
    
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        scrollView.keyboardDismissMode = .onDrag
    }
    
}

extension MCChatGPTIMVc:QMUITextViewDelegate {
    func textView(_ textView: QMUITextView!, newHeightAfterTextChanged height: CGFloat) {
        let iheight = fmax(height,  50)
        let needsChangeHeight = CGRectGetHeight(textView.frame) != iheight
        if needsChangeHeight == true {
            self.view.setNeedsLayout()
            self.view.layoutIfNeeded()
        }
    }
    
    func textView(_ textView: QMUITextView!, didPreventTextChangeIn range: NSRange, replacementText: String!) {
        
    }
    
    func textViewShouldReturn(_ textView: QMUITextView!) -> Bool {
        textView.text = nil
        return true
    }
    
}
